using Microsoft.Extensions.Logging;
using DocumentCreationSystem.Models.AgentOps;

namespace DocumentCreationSystem.Services.AgentOps
{
    /// <summary>
    /// AgentOps指标收集器接口
    /// </summary>
    public interface IAgentOpsMetricsCollector
    {
        /// <summary>
        /// 记录请求指标
        /// </summary>
        /// <param name="metrics">请求指标</param>
        void RecordRequestMetrics(RequestMetrics metrics);

        /// <summary>
        /// 记录工作流指标
        /// </summary>
        /// <param name="metrics">工作流指标</param>
        void RecordWorkflowMetrics(WorkflowMetrics metrics);

        /// <summary>
        /// 获取系统指标
        /// </summary>
        /// <returns>系统指标</returns>
        Task<SystemMetrics> GetSystemMetricsAsync();

        /// <summary>
        /// 获取性能指标
        /// </summary>
        /// <returns>性能指标</returns>
        Task<PerformanceMetrics> GetPerformanceMetricsAsync();
    }

    /// <summary>
    /// 请求指标
    /// </summary>
    public class RequestMetrics
    {
        /// <summary>
        /// 请求路径
        /// </summary>
        public string Path { get; set; } = string.Empty;

        /// <summary>
        /// HTTP方法
        /// </summary>
        public string Method { get; set; } = string.Empty;

        /// <summary>
        /// 状态码
        /// </summary>
        public int StatusCode { get; set; }

        /// <summary>
        /// 持续时间
        /// </summary>
        public TimeSpan Duration { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// AgentOps指标收集器实现
    /// </summary>
    public class AgentOpsMetricsCollector : IAgentOpsMetricsCollector
    {
        private readonly ILogger<AgentOpsMetricsCollector> _logger;

        public AgentOpsMetricsCollector(ILogger<AgentOpsMetricsCollector> logger)
        {
            _logger = logger;
        }

        public void RecordRequestMetrics(RequestMetrics metrics)
        {
            _logger.LogDebug($"记录请求指标: {metrics.Method} {metrics.Path} - {metrics.StatusCode} ({metrics.Duration.TotalMilliseconds}ms)");
        }

        public void RecordWorkflowMetrics(WorkflowMetrics metrics)
        {
            _logger.LogDebug($"记录工作流指标: {metrics.WorkflowId} - 执行时间: {metrics.ExecutionTime}");
        }

        public async Task<SystemMetrics> GetSystemMetricsAsync()
        {
            // 模拟系统指标收集
            await Task.Delay(100);
            
            return new SystemMetrics
            {
                CpuUsage = Random.Shared.NextDouble() * 0.8, // 0-80%
                MemoryUsage = 0.4 + Random.Shared.NextDouble() * 0.4, // 40-80%
                DiskUsage = 0.3 + Random.Shared.NextDouble() * 0.3, // 30-60%
                ActiveConnections = Random.Shared.Next(10, 100)
            };
        }

        public async Task<PerformanceMetrics> GetPerformanceMetricsAsync()
        {
            // 模拟性能指标收集
            await Task.Delay(100);
            
            return new PerformanceMetrics
            {
                CpuUsage = Random.Shared.NextDouble() * 0.8,
                MemoryUsage = 0.4 + Random.Shared.NextDouble() * 0.4,
                DiskUsage = 0.3 + Random.Shared.NextDouble() * 0.3,
                NetworkThroughput = new NetworkThroughput
                {
                    InboundMbps = Random.Shared.NextDouble() * 100,
                    OutboundMbps = Random.Shared.NextDouble() * 50
                },
                ResponseTime = new ResponseTimeStats
                {
                    Average = 200 + Random.Shared.NextDouble() * 300,
                    Min = 50,
                    Max = 2000,
                    P95 = 800,
                    P99 = 1500
                },
                ConcurrentConnections = Random.Shared.Next(10, 100),
                QueueLength = Random.Shared.Next(0, 20)
            };
        }
    }
}
